import 'package:shared_preferences/shared_preferences.dart';
import '../tools/generate_encrypt.dart';

// enum WalletSource {
//   // 创建
//   create = 0,
//   // 恢复
//   recover = 1,
//   // 预留
//   defluat = 2,
// }

class WalletInfo {
  static const String walletNameKey = 'walletNameKey';
  static const String storagePINKey = 'storagePINKey';
  String walletName = '';
  String pinCode = '';
  factory WalletInfo() => _getInstance();
  static WalletInfo get instance => _getInstance();

  static WalletInfo _instance;
  WalletInfo._internal() {
    /// 初始化
  }

  static WalletInfo _getInstance() {
    if (_instance == null) {
      _instance = WalletInfo._internal();
    }
    return _instance;
  }

  static void saveWalletName() async {
    String tempName = WalletInfo().walletName;
    if (tempName == null || tempName.length == 0) {
      return;
    }
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString(WalletInfo.walletNameKey, tempName);
  }

  static Future<String> getWalletName() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String walletName = prefs.getString(WalletInfo.walletNameKey) ?? '';
    return walletName;
  }

  static void savePinCodeName() async {
    String tempCode = WalletInfo().pinCode;
    if (tempCode == null || tempCode.length == 0) {
      return;
    }
    SharedPreferences prefs = await SharedPreferences.getInstance();

    /// 存入32位的MD5加签
    String tempMD5 = GenerateEncrypt.generateMD5(tempCode);
    print('tempMD5---$tempMD5');
    await prefs.setString(WalletInfo.storagePINKey, tempMD5);
  }

  static Future<String> getPinCodeName() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String pinCode = prefs.getString(WalletInfo.storagePINKey) ?? '';
    print('getPinCodeName--$pinCode');
    return pinCode;
  }
}
